package cn.org.tpeach.nosql.redis.bean;


import cn.org.tpeach.nosql.enums.RedisTreeType;
import cn.org.tpeach.nosql.tools.DataStructureUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author tyz
 * @Title: RedisTreeItem
 * @ProjectName RedisLark
 * @Description:
 * @date 2019-06-30 22:11
 * @since 1.0.0
 */
@Data
@Builder
public class RedisTreeItem {
	/**
	 * 连接id
	 */
	private String id;
	private byte[] key;
	private String name;
	private String originName;
	private RedisTreeType type;
	private String nameSpaceSepartor;
	private DbAmount dbAmount;
	public boolean delete;
	public RedisTreeItem parentItem;
	private Queue connectQueue;

	public synchronized void incrConnecting(int incr){
		if(connectQueue == null){
			connectQueue = new ConcurrentLinkedQueue<>();
		}
		if(incr > 0){
			connectQueue.offer(1);
		}else{
			if(DataStructureUtils.isNotEmpty(connectQueue)){
				connectQueue.poll();
			}
		}
	}
	public synchronized boolean isClose(){
		return DataStructureUtils.isEmpty(connectQueue);
	}


	public String getServerName(){
		RedisTreeItem parent = this;
		while (parent != null){
			if(RedisTreeType.SERVER.equals(parent.getType())){
				return parent.getName();
			}
			parent = parent.parentItem;
		}
		return "";
	}


	
}
